package com.blakequ.androidblemanager.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import com.blakequ.androidblemanager.MyApp;


public class ToastX {
    private static Toast toast;

    public static synchronized void show(final String text) {
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        toast.cancel();
                    } catch (Exception e) {

                    }
                    try {
                        toast = Toast.makeText(MyApp.getInstance(), text, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } catch (Exception e) {
                        if (e != null)
                            Log.i("toastError", e.toString() + "");
                    }
                }
            });
        } catch (Exception e) {
            if (e != null) {
                Log.e("", "" + e.toString());
            }
        }

    }

    /**
     * 该异常toast会被上传日志
     *
     * @param text
     */
    public static synchronized void showExeption(final String text) {
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        toast.cancel();
                    } catch (Exception e) {

                    }
                    try {
                        toast = Toast.makeText(MyApp.getInstance(), text, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } catch (Exception e) {
                        if (e != null)
                            Log.i("toastError", e.toString() + "");
                    }
                }
            });
        } catch (Exception e) {
            if (e != null) {
                Log.e("", "" + e.toString());
            }
        }

    }


    public static synchronized void showServiceException() {
        show("服务器异常，请稍后再试");
    }

    public static synchronized void show(Exception e) {
        if (e == null) return;
        show(e.getMessage());
    }

    public static synchronized void show(Context context, Exception e) {
        if (e == null) return;
        show(context, e.getMessage());
    }


    public static synchronized void show(final int strId) {
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        toast.cancel();
                    } catch (Exception e) {

                    }
                    try {
                        toast = Toast.makeText(MyApp.getInstance(), strId, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } catch (Exception e) {
                        if (e != null)
                            Log.i("toastError", e.toString() + "");
                    }
                }
            });
        } catch (Exception e) {
            if (e != null) {
                Log.e("", "" + e.toString());
            }
        }

    }

    public static synchronized void show(final Context conetxt, final String text) {
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        toast.cancel();
                    } catch (Exception e) {

                    }
                    try {
                        toast = Toast.makeText(conetxt, text, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } catch (Exception e) {
                        if (e != null)
                            Log.i("toastError", e.toString() + "");
                    }
                }
            });
        } catch (Exception e) {
            if (e != null) {
                Log.e("", "" + e.toString());
            }
        }

    }

    public static synchronized void show(final Context context, final int strId) {
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        toast.cancel();
                    } catch (Exception e) {

                    }
                    try {
                        toast = Toast.makeText(context, strId, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } catch (Exception e) {
                        if (e != null)
                            Log.i("toastError", e.toString() + "");
                    }

                }
            });
        } catch (Exception e) {
            if (e != null) {
                Log.e("", "" + e.toString());
            }
        }

    }

    public static synchronized void showSingle(final String text) {
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        toast = Toast.makeText(MyApp.getInstance(), text, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } catch (Exception e) {
                        if (e != null) {
                            Log.e("", "" + e.toString());
                        }
                    }

                }
            });
        } catch (Exception e) {
            if (e != null) {
                Log.e("", "" + e.toString());
            }
        }

    }


    /**
     *  独立显示（不取消之前的toast，不依赖主进程）
     * @param text
     */
    public static synchronized void showAlone(final String text) {
        try {
            toast = Toast.makeText(MyApp.getInstance(), text, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (Exception e) {
            if (e != null)
                Log.i("toastError", e.toString() + "");
        }
    }

}
